package com.qifan.alb2b.action.back;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qifan.alb2b.action.back.param.RouteQueryParam;
import com.qifan.alb2b.domain.CityInfoDomain;
import com.qifan.alb2b.domain.RouteDetailInfoDomain;
import com.qifan.alb2b.domain.RouteInfoDomain;
import com.qifan.alb2b.service.CityInfoService;
import com.qifan.alb2b.service.RouteInfoService;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.ID;
import com.qifan.alb2b.util.Page;

@Controller
@RequestMapping("/admin/route")
public class RouteManageController {
	private RouteInfoService routeInfoService;
	private CityInfoService cityInfoService;

	public void setRouteInfoService(RouteInfoService routeInfoService) {
		this.routeInfoService = routeInfoService;
	}

	public void setCityInfoService(CityInfoService cityInfoService) {
		this.cityInfoService = cityInfoService;
	}

	/**
	 * 查询线路信息
	 * @param dgm
	 * @param rqp
	 * @return
	 */
	@RequestMapping("/query.adm")
	@ResponseBody
	public Page query(DataGridModel dgm, RouteQueryParam rqp) {
		return routeInfoService.query(dgm, rqp);
	}

	/**
	 * 根据线路id查询线路详情
	 * @param routeId
	 * @return
	 */
	@RequestMapping("/queryDetail.adm")
	@ResponseBody
	public Page queryDetail(String routeId) {
		return routeInfoService.queryDetial(routeId);
	}
	
	/**
	 * 根据详情id查询详情详细信息
	 * @param detailId
	 * @return
	 */
	@RequestMapping("/queryDetailById.adm")
	@ResponseBody
	public RouteDetailInfoDomain queryDetailbyId(String detailId){
		return routeInfoService.queryDetailById(detailId);
	}

	/**
	 * 增加线路信息
	 * @param routeInfoDomain
	 * @return
	 */
	@RequestMapping("/add.adm")
	@ResponseBody
	public Map<String, Object> add(RouteInfoDomain routeInfoDomain) {
		CityInfoDomain orgCity = cityInfoService.getCityById(routeInfoDomain.getOrgCityId());
		CityInfoDomain dstCity = cityInfoService.getCityById(routeInfoDomain.getDstCityId());
		routeInfoDomain.setRouteId(ID.next() + "");
		routeInfoDomain.setOrgCityCn(orgCity.getCityCn());//设置出发城市中文名
		routeInfoDomain.setDstCityCn(dstCity.getCityCn());//设置到达城市中文名
		routeInfoDomain.setCountryId(orgCity.getCountryId());//设置出发城市所在国家
		routeInfoDomain.setCountryName(orgCity.getCountryName());//设置出发城市所在国家的中文名
		routeInfoDomain.setCreateDate(new Date());
		routeInfoService.add(routeInfoDomain);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("success", true);
		map.put("routeId", routeInfoDomain.getRouteId());
		return map;
	}
	
	/**
	 * 更新线路信息
	 * @param routeInfoDomain
	 * @return
	 */
	@RequestMapping("/update.adm")
	@ResponseBody
	public Map<String, Object> update(RouteInfoDomain routeInfoDomain){
		RouteInfoDomain orgRouteInfo=routeInfoService.queryRouteById(routeInfoDomain.getRouteId());
		orgRouteInfo.setRouteName(routeInfoDomain.getRouteName());
		orgRouteInfo.setHot(routeInfoDomain.getHot());
		orgRouteInfo.setRemark(routeInfoDomain.getRemark());
		routeInfoService.update(orgRouteInfo);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("success", true);
		return map;
	}
	
	/**
	 * 更新线路状态
	 * @param routeId
	 * @param status
	 * @return
	 */
	@RequestMapping("/updateStatus.adm")
	@ResponseBody
	public Map<String , Object> updateStatus(String routeId,int status){
		routeInfoService.updateStatus(routeId, status);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("success", true);
		return map;
	}
	
	/**
	 * 更新线路详情
	 * @param rdid
	 * @return
	 */
	@RequestMapping("/updateDetail.adm")
	@ResponseBody
	public Map<String, Object> updateDetail(RouteDetailInfoDomain rdid){
		CityInfoDomain city = cityInfoService.getCityById(rdid.getCityId());
		rdid.setCityCn(city.getCityCn());//更新目的城市中文名
		routeInfoService.updateDetail(rdid);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("success", true);
		return map;
	}
	
	/**
	 * 删除线路，对于已有订单的线路不允许删除
	 * @param routeId
	 * @return
	 */
	@RequestMapping("/del.adm")
	@ResponseBody
	public Map<String, Object> delete(String routeId){
		routeInfoService.deleteRoute(routeId);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("success", true);
		return map;
	}
}
